package work.work40;

import java.util.Scanner;

/**
 * Atm类
 */
public class Atm {
    private User [] users = {
            new User("aaaaaa", "111111", 1000),
            new User("bbbbbb", "222222", 2000),
            new User("cccccc", "333333", 3000),
            new User("dddddd", "444444", 4000),
            new User("eeeeee", "555555", 5000)
    };
    //空方法
    private Atm(){}

    private Scanner scanner = new Scanner(System.in);
    //记录登录成功是第几个账户
    private int index;
    // 控制ATM流程的方法
    public  void process(){
        System.out.println("欢迎使用lovo银行");
        while (true) {
            login();
            menu();
        }
    }
    //菜单选择
    public void menu(){
        while (true) {
            System.out.println("1.存款        2.取款");
            System.out.println("3.查询        4.转账");
            System.out.println("5.切换账号     6.退出");
            System.out.println("7.修该密码");
            System.out.println("请选择：");
            int opt = scanner.nextInt();
            if (opt == 1) {
                save();
            } else if (opt == 2) {
                get();
            } else if (opt == 3) {
                search();
            } else if (opt == 4) {
                transfer();
            } else if (opt == 5) {
                System.out.println("退出登录");
                return;
            } else if (opt == 6) {
                System.out.println("谢谢使用");
                System.exit(0);
            } else if (opt == 7) {
                if (changePwd()) {
                    System.out.println("已登出");
                    return;
                }
            } else {
                System.out.println("输入错误");
            }
        }
    }
    // 登录方法
    public  void login(){
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入账号：");
            String accountNumber = scanner.next();
            System.out.println("请输入密码：");
            String password = scanner.next();
            for (int j = 0; j < users.length; j++) {
                if (users[j].getAccountNumber().equals(accountNumber) && users[j].getPassword().equals(password)){
                    System.out.println("登录成功");
                    index = j;
                    return;
                }
            }
            System.out.println("账号或密码错误,请重新登录，还剩" + (2 - i) + "次");
        }
        System.out.println("次数用完，卡已被吞！");
        System.exit(0); //系统结束
    }

    // 存款
    public  void save(){
        System.out.println("请输入你要存储的金额：");
        double money = scanner.nextDouble();
        users[index].setMoney(users[index].getmoney() + money);
        System.out.println("存款成功");
    }
    // 取款
    public  void get(){
        System.out.println("请输入你要取款的金额：");
        double money = scanner.nextDouble();
        if(money > users[index].getmoney()){
            System.out.println("余额不足");
        }else {
            users[index].setMoney(users[index].getmoney() - money);
            System.out.println("取款成功");
        }
    }
    // 查询
    public  void search(){
        System.out.println("你的余额为：" + users[index].getmoney());
    }
    // 转账
    public  void transfer(){
        System.out.println("请输入你要转账的账号");
        String accountNumber = scanner.next();

        if (accountNumber.equals(users[index].getAccountNumber())){
            System.out.println("不能转账给自己");
            return;
        }
        for(int i = 0;i < users.length;i++){
            if(accountNumber.equals(users[i].getAccountNumber())){
                System.out.println("请输入转账金额");
                double money = scanner.nextDouble();
                if (money > users[index].getmoney()){
                    System.out.println("余额不足");
                    return;
                }else {
                    //把钱从自己账户扣除
                    users[index].setMoney(users[index].getmoney() - money);
                    // 把钱加到对方账户上
                    users[i].setMoney(users[i].getmoney() + money);
                    System.out.println("转账成功");
                    return;
                }
            }
        }
        System.out.println("转账账号错误");
    }
    // 修改密码
    public  boolean changePwd(){
        System.out.println("请输入原密码：");
        String password = scanner.next();
        if(!password.equals(users[index].getPassword())){
            System.out.println("原密码输入错误");
            return false;
        }
        System.out.println("请输入新密码：");
        String newPassword = scanner.next();
        if(newPassword.equals(users[index].getPassword())){
            System.out.println("新密码不能和原密码一样");
            return false;
        }
        System.out.println("请再次输入新密码：");
        String conPassword = scanner.next();
        if(!newPassword.equals(conPassword)){
            System.out.println("两次密码不一致");
            return false;
        }
        users[index].setPassword(newPassword);
        System.out.println("修改成功");
        return true;
    }

    public static void main(String[] args) {
        Atm atm = new Atm();
        atm.process();
    }
}
